package com.markerhub.controller;

import com.markerhub.common.lang.Result;
import com.markerhub.handler.Back_token_Auth;
import com.markerhub.handler.Wx_Token_Auth;
import com.markerhub.service.Tb_userService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author by MaXiaofeng
 * @Description 后台管理员控制类
 * @Date 2022/2/22 19:33
 * @Version 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/houtai_user")
public class Tb_userController {
    @Autowired
    private Tb_userService tb_userService;

    /**
     * 后台管理员登陆
     *
     * @param jsonBody
     * @return
     */
    @PostMapping("login")
    public Result login(@RequestBody Map<String, String> jsonBody) {
        String username = jsonBody.get("username");
        String password = jsonBody.get("password");
        if (username == null || password == null) {
            return Result.fail("登录名或密码为空，请重试");
        }
        Result result = tb_userService.login(username, password);
        log.info("{}", result);
        return result;
    }

    /**
     * 后台管理员根据用户id更改用户密码
     *
     * @param jsonBody
     * @return
     */
    @Back_token_Auth
    @PostMapping("updateUser")
    public Result updateUser(@RequestBody Map<String, String> jsonBody) {

        String userId = jsonBody.get("userId");
        String username = jsonBody.get("username");
        String password = jsonBody.get("newPassword");
        if (username == null || password == null || userId == null) {
            return Result.fail("登录名或密码为空，请重试");
        }

        Result result = tb_userService.updateUser(Integer.parseInt(userId), username, password);
        log.info("{}", result);
        return result;
    }

}
